/*
 * @Date: 2024-01-09 14:35:31
 * @LastEditors: lyx
 * @LastEditTime: 2024-01-09 14:37:44
 * @FilePath: \chlor-alkali-cloud-mobile-end\src\utils\requestFormat\resultFormat.ts
 * @Description: 
 */
// import type { RequestOptionsInit } from '../request'
/**
 * 格式化返回数据
 */
export interface RequestListType<ListT> {
	count: number;
	list: ListT;
}
export interface RequestRecordsType<RecordsT> {
	current: number;
	pages: number;
	total: number;
	records: RecordsT;
}
export interface RequestResultType<ContentT> {
	isSuccess: boolean;
	code: string;
	msg: string;
	content?: ContentT;
	requestId?: string;
}

export interface RequestFormatResultType<ContentT> {
	status: boolean;
	code: string;
	msg: string;
	content: ContentT;
	res?: RequestResultType<ContentT>;
	response?: Response;
}

export interface RequestDataResultType {
	data: ArrayBuffer | Blob | FormData | string;
	response: Response;
}

const resultFormat = (res: RequestResultType<unknown>, options: any, response: Response | undefined): RequestFormatResultType<unknown> => {
	let { isSuccess, code, msg: errMsg, content } = res;
	const { getResult, getResponse } = options
	// 国际化开启返回code码
	let msg: string = errMsg;
	// 响应状态
	let status = !!isSuccess && code === "00000";
	//	响应不成功，处理返回的响应消息
	if (!status) {
		msg = errMsg;
	}
	const obj: RequestFormatResultType<unknown> = {
		status,
		code,
		msg,
		content,
	}
	if (getResult) obj.res = res
	if (getResponse === true) obj.response = response
	return obj;
};

export default resultFormat;
